/**
 * Copyright (c) 2018 小润科技 All rights reserved.
 * <p>
 * 版权所有，侵权必究！
 */

package com.xr.sy.modules.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xr.common.annotation.DataFilter;
import com.xr.common.constant.Constant;
import com.xr.common.page.PageData;
import com.xr.common.service.impl.BaseServiceImpl;
import com.xr.common.service.impl.CrudServiceImpl;
import com.xr.common.utils.ConvertUtils;
import com.xr.sy.modules.business.dao.FxTzggDao;
import com.xr.sy.modules.business.dao.NoticeDao;
import com.xr.sy.modules.business.dto.FxTzggDTO;
import com.xr.sy.modules.business.dto.NoticeDTO;
import com.xr.sy.modules.business.entity.FxTzggEntity;
import com.xr.sy.modules.business.entity.NoticeEntity;
import com.xr.sy.modules.business.entity.SysMessageEntity;
import com.xr.sy.modules.business.service.FxTzggService;
import com.xr.sy.modules.business.service.NoticeService;
import com.xr.sy.modules.message.dto.SysSmsDTO;
import com.xr.sy.modules.message.entity.SysSmsEntity;
import com.xr.sy.modules.security.user.SecurityUser;
import com.xr.sy.modules.security.user.UserDetail;
import com.xr.sy.modules.sys.service.SysDeptService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service

public class NoticeServiceImpl extends CrudServiceImpl<NoticeDao, NoticeEntity, NoticeDTO> implements NoticeService {
    @Autowired
    private SysDeptService sysDeptService;

    @Override
    public QueryWrapper<NoticeEntity> getWrapper(Map<String, Object> params) {


        String title = (String) params.get("title");
        QueryWrapper<NoticeEntity> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(title), "title", title);

        UserDetail u = SecurityUser.getUser();
        if(null != u.getDeptId()){
            List<Long> deptIds = sysDeptService.getSubDeptIdList(u.getDeptId());
            wrapper.in("dept_id", deptIds);
        }

        wrapper.orderByDesc("pub_date");

        return wrapper;
    }

    @Override
    public PageData<NoticeDTO> page(Map<String, Object> params) {
        IPage<NoticeEntity> page = baseDao.selectPage(getPage(params, Constant.CREATE_DATE, false), getWrapper(params));
        return getPageData(page, NoticeDTO.class);
    }

    @Override
    public NoticeDTO latest() {
        //查询
        NoticeEntity entity = baseDao.latest();
        return ConvertUtils.sourceToTarget(entity, NoticeDTO.class);
    }

    @Override
    public NoticeDTO get(Long id) {
        NoticeEntity entity = baseDao.selectById(id);
        return ConvertUtils.sourceToTarget(entity, NoticeDTO.class);
    }

    @Override
    public void save(NoticeDTO dto) {
        NoticeEntity entity = ConvertUtils.sourceToTarget(dto, NoticeEntity.class);

        insert(entity);
    }

    @Override
    public void update(NoticeDTO dto) {
        NoticeEntity entity = ConvertUtils.sourceToTarget(dto, NoticeEntity.class);

        updateById(entity);
    }

}
